package cn.tannn.ops.pwd.service.impl;

import cn.tannn.jdevelops.jpa.service.J2ServiceImpl;
import cn.tannn.ops.pwd.dao.KeePassDao;
import cn.tannn.ops.pwd.entity.KeePassEntity;
import cn.tannn.ops.pwd.enums.KeePassStatus;
import cn.tannn.ops.pwd.service.KeePassService;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 密码管理器
 *
 * @author tnnn
 * @version V1.0
 * @date 2023-02-02 09:53
 */
@Service
public class KeePassServiceImpl extends J2ServiceImpl<KeePassDao, KeePassEntity, Long> implements KeePassService {
    public KeePassServiceImpl() {
        super(KeePassEntity.class);
    }

    @Override
    public Optional<KeePassEntity> findById(Long id) {
        return getJpaBasicsDao().findById(id);
    }

    @Override
    public void deleteById(KeePassStatus status, Long id) {
        try {
            if (KeePassStatus.RECYCLED == status) {
                getJpaBasicsDao().deleteById(id);
            } else if (KeePassStatus.VALID == status) {
                getJpaBasicsDao().changeStatus(id, KeePassStatus.RECYCLED);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
